﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Common;


namespace Model
{
    /// <summary>
    /// 菜单
    /// </summary>
    public class SysMenuTreeNodeCollection
    {
        IEnumerable<SysMenuInfo> listTree;
        /// <summary>
        /// 编辑展示菜单
        /// </summary> 
        /// <returns></returns>
        public bool Bind(IEnumerable<SysMenuInfo> entitys, string myParentId, ref List<SystemTree> myChildren)
        {
            if (null != myParentId)
                listTree = from o in entitys
                           where o.ParentId == myParentId
                           orderby o.Sort
                           select o;//叶子节点            
            else
                listTree = from o in entitys
                           where o.ParentId == null || o.ParentId == ""
                           orderby o.Sort
                           select o;//根目录

            if (listTree != null && listTree.Any())
            {//填充数据
                foreach (var item in listTree)
                {
                    SystemTree myTree = new SystemTree() { id = item.Id.GetString(), text = item.Name.GetString() };
                    //if (string.IsNullOrWhiteSpace(item.Status))
                    //    myTree.@checked = false;
                    //else
                    //    myTree.@checked = true;
                    //if (!string.IsNullOrWhiteSpace(item.Iconic))
                    //    myTree.iconCls = item.Iconic;//开启图标

                    myTree.attributes = new SystemTree.Attributes { url = item.Url.GetString() };

                    myChildren.Add(myTree);
                    if (Bind(entitys, item.Id, ref myTree.children))//递归调用
                    {
                        myTree.state = item.State;
                        if (null != item.ParentId)
                        {//根目录
                            myTree.iconCls = "icon-ok";//如果包含此字符串，则点击查看全部
                            // myTree.state = "open";//默认是打开还是关闭
                            myTree.@checked = true;
                        }
                        else
                        {
                            //myTree.state = "closed";
                        }
                    }
                }
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 展示菜单树
        /// </summary> 
        /// <returns></returns>
        public bool Bind(IEnumerable<SysMenuInfo> entitys, string myParentId, ref List<SystemTree> myChildren, DataTable dt, DBDeploy db)
        {
            if (null != myParentId)
                listTree = from o in entitys
                           where o.ParentId == myParentId
                           orderby o.Sort
                           select o;//叶子节点            
            else
                listTree = from o in entitys
                           where o.ParentId == null || o.ParentId == ""
                           orderby o.Sort
                           select o;//根目录

            if (listTree != null && listTree.Any())
            {//填充数据
                foreach (var item in listTree)
                {
                    string urlPara = "?dbName=" + db.DBName;
                    if (db.DBStatus == 2)
                        urlPara += string.Format("&DBStatus={0}", db.DBStatus);
                    if (db.DBType == 2)
                        urlPara += "&DBType=2";
                    if (!(db.DBType == 2 && item.Name == "模型管理"))
                    {
                        bool flg = false;
                        for (int j = 0; j < dt.Rows.Count; j++)
                        {
                            string MunItem = DBConvert.GetAnsiString(dt.Rows[j], "MunItem");
                            if (MunItem == item.Id.GetString() || MunItem == item.Url.GetString())
                            {
                                flg = true;
                            }
                        }

                        SystemTree myTree = new SystemTree() { id = item.Id.GetString(), text = item.Name.GetString() };


                        myTree.attributes = new SystemTree.Attributes { url = item.Url.GetString() + urlPara };
                        myTree.rootText = db.DisplayName;
                        myChildren.Add(myTree);
                        if (flg)
                            myTree.@checked = true;
                        if (Bind(entitys, item.Id, ref myTree.children, dt, db))//递归调用
                        {
                            myTree.state = item.State;
                        }
                    }
                }
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
